package pers.bc.utils.test;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;

import pers.bc.utils.file.StreamUtil;
import pers.bc.utils.pub.JudgeAssertUtil;
import pers.bc.utils.pub.LoggerUtil;
import pers.bc.utils.pub.PubEnvUtil;
import pers.bc.utils.pub.RegUtil;
import pers.bc.utils.pub.StringUtil;

public class StringUt
{
    public static void main(String[] args) throws Exception
    {
        ClassCastException ecc = new ClassCastException("YYYYYYY");
        NoClassDefFoundError e = new NoClassDefFoundError("**************");
        e.initCause(ecc);
        // e.addSuppressed(ecc);
        RuntimeException retExceptopn = new RuntimeException("👉" + e.getMessage());
        // retExceptopn.setStackTrace(e.getStackTrace());
        // retExceptopn.addSuppressed(e);
        retExceptopn.initCause(e);
        
         System.out.println(StringUtil.toString( retExceptopn));
//        JudgeAssertUtil.throwException(e, "mylogs", e.getMessage());
        // String sql = "12323 (vs";
        // System.out.println(sql);
        // if (sql.contains("23\\s+(vs"))//
        // sql = sql.replaceAll("23(s\\s+)(vs", "23(vs");
        // System.out.println(sql);
        // sql = sql.replaceAll("(\\w+)\\s(\\w+)", "@@");
        // System.out.println(sql);
        //// String originalString = "这是 示例 字符串。";
        //// String replacedString = originalString.replaceAll("\\s+", ""); // 替换所有空白字符为一个空字符串
        //// System.out.println(replacedString); // 输出: 这是示例字符串。
        //
        // String originalString = "这是 示例 字符串。";
        // String pattern = "(\\w+)\\s(\\w+)"; // 匹配单词和单词之间的单个空格
        // String replacement = "$1 $2"; // 用两个空格替换单个空格
        // String replacedString = originalString.replaceAll(pattern, replacement);
        // System.out.println(replacedString); // 输出: 这是 示例 字符串。
        
        // BigDecimal decimal2 = new BigDecimal("").divide(new BigDecimal("100.00"));
        // System.out.println(decimal2);
        
        // BigDecimal count = BigDecimal.ZERO;
        // BigDecimal pay = null;
        // BigDecimal pays = pay == null ? BigDecimal.ZERO : pay;
        // // System.out.println( pays.add(null));
        //
        // BigDecimal bigDecimal = new BigDecimal(1816160.00);
        // BigDecimal decimal = new BigDecimal(774279.00);
        // BigDecimal divideStr = new BigDecimal(774279.00).divide(new BigDecimal(1816160.00));
        // BigDecimal divide = decimal.divide(bigDecimal, 2);
        // BigDecimal multiply = divide.multiply(new BigDecimal(100));
        // System.out.println(divide);
        // System.out.println(multiply);
        // System.out.println(774279/1816160);
        //
        // System.err.println("保留1位计算结果："+ decimal.divide(bigDecimal, 1,BigDecimal.ROUND_HALF_UP));
        // System.err.println("保留2位计算结果："+ decimal.divide(bigDecimal, 2,BigDecimal.ROUND_HALF_UP));
        // System.err.println("保留3位计算结果："+ decimal.divide(bigDecimal, 3,BigDecimal.ROUND_HALF_UP));
        
    }
    
    public static void mainw(String[] args)
    {
        try
        {
            // UFDate before = new UFDate().getDateBefore(0);
            // UFDate before2 = new UFDate().getDateBefore(1);
            // System.out.println(before);
            // System.out.println(before2);
            // System.out.println(before2.compareTo(before) < 0);
            // String lyflbh = String.format("%04d", 1);
            // System.out.println(lyflbh);
            // lyflbh = String.format("%04d", 2);
            // System.out.println(lyflbh);
            // lyflbh = String.format("%04d", 1256);
            // System.out.println(lyflbh);
            
            // 1.源代码阶段:java文件编译为.class文件 获取方法:Class.forName(String className)
            
            // String fileName = "feihe";
            // String fileName = "zhonguojianzhu";
            String fileName = "aimer";
            String fileStr = "D:\\WorkSpaceYonyou\\" + fileName + ".json";
            byte[] in = StreamUtil.readFileToByteArray(fileStr);
            String json = StreamUtil.InputStreamTOString(StreamUtil.byteTOInputStream(in));
            System.out.println(json);
            // Map<String, Object> obj = JsonUtil.toMap(json);
            // JSONObject obj=JSONObject.fromObject(json);
            JSONArray obj = JSONArray.parseArray(json);
            // List<Map<String, Object>> obj = JsonUtil.toList(json);
            System.out.println(obj);
            String vcf = savaVcfUpper(obj, fileName);
            vcf += savaVcfLower(obj, fileName);
            System.out.println(vcf);
            // select code,'zbk'||name as name ,mobile,email from bd_psndoc where
            // length(mobile)=11
            
            LoggerUtil.getInstance().debug(vcf);
            StreamUtil.writeByteArrayToFile(new File(
                // "D:\\WorkSpaceYonyou\\shuiwu.vcf"), StreamUtil
                "D:\\WorkSpaceYonyou\\" + fileName + ".vcf"), StreamUtil.StringTObyte(vcf));
        }
        catch (Exception e)
        {
            LoggerUtil.thr(e);
        }
    }
    
    static String savaVcfUpper(List obj, String org)
    {
        StringBuilder vcf = new StringBuilder();
        for (Object jsonObj : obj)
        {
            Map map = (Map) jsonObj;
            String mobile = StringUtil.toString(map.get("MOBILE")).intern();
            String name = StringUtil.toString(map.get("NAME")).intern();
            String code = "zgjz" + StringUtil.toString(map.get("CODE")).intern();
            String email = StringUtil.toString(map.get("EMAIL")).intern();
            if (!(name.contains("测试") || name.contains("ceshi") || code.contains("ceshi")))
            {
                if (RegUtil.isMobileNO(mobile))
                {
                    vcf.append("BEGIN:VCARD").append(PubEnvUtil.CRLF);
                    vcf.append("VERSION:2.1").append(PubEnvUtil.CRLF);
                    vcf.append("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(name).append(";;;;").append(PubEnvUtil.CRLF);
                    vcf.append("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(name).append(PubEnvUtil.CRLF);
                    vcf.append("TEL;CELL:").append(mobile).append(PubEnvUtil.CRLF);
                    if (RegUtil.isEmail(email))
                    {
                        vcf.append("EMAIL;HOME:").append(email).append(PubEnvUtil.CRLF);
                    }
                    vcf.append("NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(code).append(PubEnvUtil.CRLF);
                    vcf.append("X-GROUP-MEMBERSHIP:").append(org).append(PubEnvUtil.CRLF);
                    vcf.append("END:VCARD").append(PubEnvUtil.CRLF);
                }
            }
        }
        
        return StringUtil.toString(vcf);
    }
    
    static String savaVcfLower(List obj, String org)
    {
        StringBuilder vcf = new StringBuilder();
        for (Object jsonObj : obj)
        {
            Map map = (Map) jsonObj;
            String mobile = StringUtil.toString(map.get("mobile")).intern();
            String name = StringUtil.toString(map.get("name")).intern();
            String code = "zgjz" + StringUtil.toString(map.get("code")).intern();
            String email = StringUtil.toString(map.get("email")).intern();
            if (!(name.contains("测试") || name.contains("ceshi") || code.contains("ceshi")))
            {
                if (RegUtil.isMobileNO(mobile))
                {
                    vcf.append("BEGIN:VCARD").append(PubEnvUtil.CRLF);
                    vcf.append("VERSION:2.1").append(PubEnvUtil.CRLF);
                    vcf.append("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(name).append(";;;;").append(PubEnvUtil.CRLF);
                    vcf.append("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(name).append(PubEnvUtil.CRLF);
                    vcf.append("TEL;CELL:").append(mobile).append(PubEnvUtil.CRLF);
                    if (RegUtil.isEmail(email))
                    {
                        vcf.append("EMAIL;HOME:").append(email).append(PubEnvUtil.CRLF);
                    }
                    vcf.append("NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(code).append(PubEnvUtil.CRLF);
                    vcf.append("X-GROUP-MEMBERSHIP:").append(org).append(PubEnvUtil.CRLF);
                    vcf.append("END:VCARD").append(PubEnvUtil.CRLF);
                }
            }
        }
        
        return StringUtil.toString(vcf);
    }
    
    static String exeVcf2(List obj)
    {
        StringBuilder vcf = new StringBuilder();
        for (Object jsonObj : obj)
        {
            Map map = (Map) jsonObj;
            String mobile = StringUtil.toString(map.get("mobile")).intern();
            String name = StringUtil.toString(map.get("name")).intern();
            String code = StringUtil.toString(map.get("code")).intern();
            String email = StringUtil.toString(map.get("email")).intern();
            if (RegUtil.isMobileNO(mobile))
            {
                vcf.append(getVcf().replace("{mobile}", mobile).replace("{name}", name).replace("{email}", email).replace("{code}", code));
            }
        }
        
        return StringUtil.toString(vcf);
    }
    
    static String getVcf()
    {
        StringBuilder str = new StringBuilder();
        str.append("BEGIN:VCARD").append(PubEnvUtil.CRLF);
        str.append("VERSION:2.1").append(PubEnvUtil.CRLF);
        str.append("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{name};;;;").append(PubEnvUtil.CRLF);
        str.append("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{name}").append(PubEnvUtil.CRLF);
        str.append("TEL;CELL:{mobile}").append(PubEnvUtil.CRLF);
        str.append("EMAIL;HOME:{email}").append(PubEnvUtil.CRLF);
        // str.append("ORG:fhry").append(PubEnvUtil.CRLF);
        str.append("NOTE;CHARSET=UTF-8;ENCODING{code}").append(PubEnvUtil.CRLF);
        str.append("X-GROUP-MEMBERSHIP:fhry").append(PubEnvUtil.CRLF);
        str.append("END:VCARD").append(PubEnvUtil.CRLF);
        
        return StringUtil.toString(str);
        
    }
}
